#!coding:utf-8
'''
Created on Nov 12, 2012

@author: join
'''

from thrift import Thrift
from thrift.transport import TSocket
from thrift.protocol import TCompactProtocol
from thrift.transport import TTransport
from lottery_terminal.rpc.gen_py.thrift_protocal.LotterySerivice import Client

class ThirdpartyAPI(object):
    def __init__(self, lottery):
        self.lottery = lottery
        super(ThirdpartyAPI, self).__init__()
        
    def __getattr__(self, name):
        self.contorlet = _getContorlet()
        if hasattr(self.contorlet,name):
            return getattr(self.contorlet,name)
        raise Exception('attr not found')

def _getContorlet():
    try:
        # Make socket
        host, port = '127.0.0.1',9000
        
        transport = TSocket.TSocket(host, int(port))
        # use TFramedTransport
        transport = TTransport.TFramedTransport(transport)
        # Wrap in a protocol use TCompactProtocol
        protocol = TCompactProtocol.TCompactProtocol(transport)
        # Create a client to use the protocol encoder
        client = Client(protocol)
        # Connect!
        transport.open()
    except Thrift.TException, tx:
        print '%s' % (tx.message)
    return client
 
 
API = ThirdpartyAPI(_getContorlet())
    